import collections
class Solution(object):
    def subdomainVisits(self, cpdomains):
        """
        :type cpdomains: List[str]
        :rtype: List[str]
        """
        d = collections.defaultdict(int)
        for p in cpdomains:
            p = p.split(' ')
            c = int(p[0])
            domain = p[1].split('.')
            if len(domain) >= 1:
                d[domain[-1]] += c
            if len(domain) >= 2:
                d['.'.join(domain[-2:])] += c
            if len(domain) == 3:
                d[p[1]] += c
        out = []
        for key in d:
            out.append('{} {}'.format(d[key], key))
        return out




data = Solution()
cpdomains = ["900 google.mail.com", "50 yahoo.com", "1 intel.mail.com", "5 wiki.org"]
print(data.subdomainVisits(cpdomains))
